package com.jd.pet.controller;

import com.jd.pet.domain.PetDetail;
import com.jd.pet.service.IPetDetailService;
import com.jd.pet.service.IPetService;
import com.jd.pet.domain.Pet;
import com.jd.pet.query.PetQuery;
import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.PageList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    public IPetService petService;
    @Autowired
    private IPetDetailService petDetailService;


    /**
     * 保存和修改公用的
     * @param pet  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Pet pet){
        try {
            if( pet.getId()!=null)
                petService.update(pet);
            else
                petService.add(pet);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            petService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Pet get(@PathVariable("id")Long id)
    {
        Pet pet = petService.findById(id);
        System.err.println("查询信息测试" + pet);
        return petService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Pet> list(){

        return petService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Pet> json(@RequestBody PetQuery query)
    {
        PageList<Pet> petPageList = petService.queryByPage(query);
        System.err.println("分页++++++++++++++++++++++++++" + petPageList);
        return petService.queryByPage(query);
    }

    /**
     * 宠物上架
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids) {
        petService.onsale(ids);
        return AjaxResult.me().success();
    }
}
